package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.product.service.BaseCategoryService;
import com.google.common.collect.Lists;
import java.util.Date;


import com.atguigu.gmall.feign.list.SearchFeignClient;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.SkuAttrValueService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.vdo.ValueJsonDo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.hash.BloomFilter;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Dell
* @description 针对表【sku_info(库存单元表)】的数据库操作Service实现
* @createDate 2022-05-19 10:00:50
*/
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
implements SkuInfoService{

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;

    @Autowired
    private SkuSaleAttrValueMapper saleAttrValueMapper;

    @Autowired
    private BaseSpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private BaseTrademarkMapper trademarkMapper;

    @Autowired
    private BaseCategoryService baseCategoryService;

    /*@Qualifier("bloomFilter")
    @Autowired*/
    private RBloomFilter<Object> bloomFilters;

    @Override
    @Transactional
    public void saveSkuInfo(SkuInfo skuInfo) {
        //保存库存单元信息
        baseMapper.insert(skuInfo);
        //在布隆过滤器中添加一份ID
        bloomFilters.add(skuInfo.getId());


        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        for (SkuAttrValue skuAttrValue : skuAttrValueList) {
            //保存sku平台属性值
            skuAttrValue.setSkuId(skuInfo.getId());
            skuAttrValueMapper.insert(skuAttrValue);
        }

        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        for (SkuImage skuImage : skuImageList) {
            skuImage.setSkuId(skuInfo.getId());
            //保存sku平台商品图片
            skuImageMapper.insert(skuImage);
        }

        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        for (SkuSaleAttrValue skuSaleAttrValue : skuSaleAttrValueList) {
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
            //保存sku销售属性值
            saleAttrValueMapper.insert(skuSaleAttrValue);
        }

    }

    @Override
    public BigDecimal getPrice(Long skuId) {
        return baseMapper.getPrice(skuId);
    }

    @Override
    public List<SpuSaleAttr> getSkuSaleAttrAndValue(Long skuId) {
        return spuSaleAttrMapper.getSkuSaleAttrAndValue(skuId);
    }

    @Override
    public Map<String, String> getValuesSkuJson(Long skuId) {
        Map<String, String> map = new HashMap<>();
        List<ValueJsonDo> skuJson = spuSaleAttrMapper.getValuesSkuJson(skuId);
        for (ValueJsonDo valueJsonDo : skuJson) {
            map.put(valueJsonDo.getSaleAttrValue(),valueJsonDo.getId().toString());
        }
        return map;
    }

    @Override
    public List<Long> getAllSkuId() {
        return baseMapper.getAllSkuId();
    }

    @Override
    public void onOrcanceSale(Long skuId, int i) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(i);
        baseMapper.updateById(skuInfo);
        if (i == 1){
            //上架商品，保存到ES中
            Goods goods = this.getSearchGoods(skuId);
            //保存ES中
            searchFeignClient.searchProduct(goods);
        }

        if (i == 0){
            //下架商品，从ES中删除
            searchFeignClient.deleteSearchProduct(skuId);
        }
    }

    @Override
    public Goods getSearchGoods(Long skuId) {
        Goods goods = new Goods();
        //1、获取sku基本信息
        SkuInfo skuInfo = baseMapper.selectById(skuId);
        goods.setId(skuInfo.getId());
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date());

        //2、获取品牌信息
        BaseTrademark trademark = trademarkMapper.selectById(skuInfo.getTmId());
        goods.setTmId(trademark.getId());
        goods.setTmName(trademark.getTmName());
        goods.setTmLogoUrl(trademark.getLogoUrl());
        //3、查询三级分类信息
        BaseCategoryView categoryView = baseCategoryService.getSkuCategoryView(skuId);
        goods.setCategory1Id(categoryView.getCategory1Id());
        goods.setCategory1Name(categoryView.getCategory1Name());
        goods.setCategory2Id(categoryView.getCategory2Id());
        goods.setCategory2Name(categoryView.getCategory2Name());
        goods.setCategory3Id(categoryView.getCategory3Id());
        goods.setCategory3Name(categoryView.getCategory3Name());
        //4、商品热度
        goods.setHotScore(0L);
        //5、平台属性
        List<SearchAttr> searchAttrs = skuAttrValueMapper.getAttrAll(skuInfo.getId());
        goods.setAttrs(searchAttrs);

        return goods;
    }
}
